<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopShippingMethodBean {

    function BWebshopShippingMethodBean() {
    }	
    
    function getAllShippingMethods($db) {
		$sql = "SELECT s.*, c.symbol_left FROM " . TBL_SHIPPINGMETHOD . " s JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id";
    	$res = $db->get_results($sql);
    	
    	return $res;
	}
	
	function getShippingMethodsByCode($db, $code) {
		$sql = "SELECT s.*, c.symbol_left FROM " . TBL_SHIPPINGMETHOD . " s JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id WHERE s.code = '$code'";
    	$res = $db->get_row($sql);
    	
    	return $res;
	}
	
	function validateShippingMethod() {
    	$msg = "";	
    	
		if(trim($_POST['code']) == "") {
			if($msg != "") $msg .= "<br />";
			$msg .= _("You must enter the code.");
		}
		
		if(trim($_POST['name']) == "") {
			if($msg != "") $msg .= "<br />";
			$msg .= _("You must enter the name.");
		}
		
		if(trim($_POST['delivery_cost']) == "") {
			if($msg != "") $msg .= "<br />";
			$msg .= _("You must enter the cost.");
		}
		
		if(trim($_POST['delivery_cost']) != "" && !is_numeric($_POST['delivery_cost'])) {
			if($msg != "") $msg .= "<br />";
			$msg .= _("Invalid cost, cost should be a numeric value.");
		}
		
		return $msg;
    }
	
	function saveShippingMethod($db, $method) {
		$sql= "INSERT INTO ".TBL_SHIPPINGMETHOD."(code,name,delivery_cost,currency_id,status)".
					"VALUES('".$method['code']."','".$method['name']."','".
		$method['delivery_cost']."','".$method['currency_id']."','".
		$method['status']."')";
    	$res = $db->query($sql);
		$id = -1;
		if($res == true) {	
			$id=$method['code'];
		}	
    	return $id;
	}
	
	function deleteShippingMethodByCode($db, $id) {
    	$sql = sprintf("DELETE FROM %1\$s " .
    			"WHERE code='$id'", TBL_SHIPPINGMETHOD);		
    	$res = $db->query($sql);
    	return $res;
    }
	
	function updateShippingMethod($db, $method, $code) {
		$sql = "UPDATE " . TBL_SHIPPINGMETHOD . " SET code = '".$method['code']."',".
    					" name = '".$method['name']."'," .
    					" delivery_cost = '".$method['delivery_cost']."'," .
    					" currency_id = '".$method['currency_id']."'," .
    					" status = '".($method['status'] != "" ? "1" : "0")."'" .
    					" WHERE code = '".$code."'";		
		$res = $db->query($sql);
    	return $res;
	}
}
?>